function pad(value: any, length: number = 2) {
  return value.toString().padStart(length, "0");
}

/**
 * 日期处理，传入时间戳
 * @param {*} timestamp 时间戳 | Date
 * @param {*} form 时间格式
 * @returns 标准时间 YYYY-MM-DD hh:mm:ss
 */
export function formatTime(timestamp: number | Date, form: string = "YYYY-MM-DD hh:mm:ss") {
  try {
    const date = new Date(timestamp);
    const year = pad(date.getFullYear(), 4);
    const month = pad(date.getMonth() + 1);
    const day = pad(date.getDate());
    const hours = pad(date.getHours());
    const minutes = pad(date.getMinutes());
    const seconds = pad(date.getSeconds());

    return form
      .replace("YYYY", year)
      .replace("MM", month)
      .replace("DD", day)
      .replace("hh", hours)
      .replace("mm", minutes)
      .replace("ss", seconds);
  } catch (error) {
    throw new TypeError(`formatTime 参数错误`);
  }
}
